package com.lanren.familybookapp.ui.me.view;

import android.os.Bundle;
import android.view.View;

import com.lanren.common.app.ActivityStackManager;
import com.lanren.common.utils.AppUtils;
import com.lanren.common.utils.Utils;
import com.lanren.common.widget.SettingBar;
import com.lanren.common.widget.TitleBarView;
import com.lanren.common.widget.statusbar.ImmersionBar;
import com.lanren.familybookapp.R;
import com.lanren.familybookapp.app.AppUserHelper;
import com.lanren.familybookapp.base.BaseUiActivity;
import com.lanren.familybookapp.helper.upgrade.AppUpgradeHelp;
import com.lanren.familybookapp.helper.upgrade.IAppUpgradeView;
import com.lanren.familybookapp.ui.me.interfaces.ISettingModel;
import com.lanren.familybookapp.ui.me.model.SettingsModel;
import com.lanren.familybookapp.ui.me.presenter.SettingPresenter;
import com.lanren.familybookapp.ui.user.view.LoginActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ClassName SettingsActivity
 * @Description TODO
 * @Author chongheng.wang
 * @Date 2019/10/11 13:56
 * @Version 1.0
 */
public class SettingsActivity extends BaseUiActivity<SettingPresenter> implements ISettingModel.ISettingView, IAppUpgradeView.AppUpgradeView {
    @BindView(R.id.title_view)
    TitleBarView titleView;
    @BindView(R.id.sb_setting_update)
    SettingBar sbSettingUpdate;
    @BindView(R.id.sb_setting_agreement)
    SettingBar sbSettingAgreement;
    @BindView(R.id.sb_setting_about)
    SettingBar sbSettingAbout;
    @BindView(R.id.sb_setting_cache)
    SettingBar sbSettingCache;
    @BindView(R.id.sb_setting_exit)
    SettingBar sbSettingExit;

    @Override
    public SettingPresenter createPresenter() {
        return new SettingPresenter(new SettingsModel());
    }

    @Override
    protected void setStatusBar() {
        mImmersionBar = ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    public void doCreate(Bundle savedInstanceState) {
        initView();
        initData();
        initEvent();
    }

    public void initView() {
        if (!AppUtils.isDebug()) {
            sbSettingAgreement.setVisibility(View.GONE);
            sbSettingAbout.setVisibility(View.GONE);
            sbSettingCache.setVisibility(View.GONE);
        }

    }

    public void initData() {
    }

    public void initEvent() {
        titleView.setBackListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @OnClick({R.id.sb_setting_update, R.id.sb_setting_agreement, R.id.sb_setting_about, R.id.sb_setting_cache, R.id.sb_setting_exit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.sb_setting_update:
                if (Utils.isFastClick(view)) {
                    checkUpgrade();
                }
                break;
            case R.id.sb_setting_agreement:
                break;
            case R.id.sb_setting_about:
                break;
            case R.id.sb_setting_cache:
                break;
            case R.id.sb_setting_exit:
                if (Utils.isFastClick(view)) {
                    AppUserHelper.getInstance().logout();
                    startActivityFinish(LoginActivity.class);
                    ActivityStackManager.getInstance().finishAllActivities(LoginActivity.class);
                }
                break;
        }
    }

    private void checkUpgrade() {
        AppUpgradeHelp.getInstance().checkVersion(this, AppUtils.getVersionName(), this);
    }

    @Override
    public void showError(int code, String msg) {
        super.showError(code, msg);
    }

}
